/*
 * 
 * Version 1.00
 * 
 * Created on 2007-6-4
 * Encoding GB2312
 */
package net.comm.group.service.impl;

import java.sql.Connection;
import java.util.Calendar;
import java.util.List;

import net.comm.check.service.impl.JifenServiceImpl;
import net.comm.common.GlbFinal;
import net.comm.common.ReturnMessage;
import net.comm.param.service.impl.SequenceMgr;
import net.comm.common.common;


import net.comm.file.service.impl.FilesServiceImpl;
import net.comm.file.web.form.FilesForm;
import net.comm.group.bean.BaseGFollForum;
import net.comm.group.bean.BaseGForum;
import net.comm.group.db.DbGFollForumService;
import net.comm.group.db.DbGForumService;
import net.comm.group.db.GFollForumServiceSqlMysql;
import net.comm.group.db.GForumServiceSqlMysql;
import net.comm.group.web.form.GFollForumForm;
import net.comm.page.PageCtr;

import net.comm.util.DatetimeUtil;
import net.comm.util.DbcpPool;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.sakaiproject.db.impl.BasicSqlService;


/**
 *
 * @author hcgong(hcgong@sina.com)
 * @version 1.0
 * @date 2007-6-4
 */
public class GFollForumServiceImpl {

	static Logger logger = Logger.getLogger(GFollForumServiceImpl.class.getName());

	/**
	 * 
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#list(net.swift.module.other.ExaQueryBean, net.swift.common.ReturnMessage)
	 */
	public List list(GFollForumForm theForm,  ReturnMessage msg) 
	{
		List list = null;

		Connection cn = null;

		try
		{
			cn = DbcpPool.getConnection();
			
			GFollForumServiceSqlMysql servSql = new GFollForumServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();

			//取得FORM中传入的查询参数
			Object[] fields2 = new Object[1];
			
			fields2[0] = theForm.getQryGForumID();
			
			/*
			 * 获得查询SQL语句
			 */
			String sql = servSql.getCountSql1();

			/**
			 * 根据SQL执行结果计算分页相关信息
			 */
			PageCtr page = theForm.getPage();
			page.setTotalNum(bSqlServ.dbCount(cn, sql, fields2));
			page.caculate();

			//取得FORM中传入的查询参数
			Object[] fields = new Object[3];
			
			fields[0] = theForm.getQryGForumID();
			fields[1] = page.getPos()-1;
			fields[2] = page.getPageSize();
			
			sql = servSql.getRecbyPageSql();
			
			list = bSqlServ.dbRead(cn, sql, fields, new DbGFollForumService());
			
		    DbcpPool.returnConnection(cn);
		    
		} catch(Exception ex) 
		{
			/**
			 * 记录相关日志
			 */
			logger.error(ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
			
		} finally {
			DbcpPool.returnConnection(cn);
		}		
		
		return list;
	}

	/**
	 * 返回根据会员ID查询
	 */
	public List list(int  userID) 
	{
		List list = null;
		Connection cn = null;

		try
		{
			cn = DbcpPool.getConnection();
			
			//取得FORM中传入的查询参数
			Object[] fields = new Object[2];

			fields[0] = userID;			
			fields[1] = DatetimeUtil.getDateByadd(DatetimeUtil.getDatetime(),
					Calendar.DAY_OF_MONTH, -7,"yyyyMMddhhmmss");			

			GFollForumServiceSqlMysql servSql = new GFollForumServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();

			/*
			 * 获得查询SQL语句
			 */
			String sql = servSql.getByUserIDSql();
									
			list = bSqlServ.dbRead(cn, sql, fields, new DbGFollForumService());
									
		    DbcpPool.returnConnection(cn);
		    
		} catch(Exception ex) 
		{
			/**
			 * 记录相关日志
			 */
			logger.error(ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return list;
	}
	
	/**
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#get(net.swift.db.tab_paramnostro, net.swift.common.ReturnMessage)
	 */
	public void get(GFollForumForm theForm, ReturnMessage msg) 
	{
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			List list = null;

			//取得FORM中传入的查询参数
			Object[] fields = new Object[1];
			
			fields[0] = theForm.getGFollowID();			
			
			GFollForumServiceSqlMysql servSql = new GFollForumServiceSqlMysql();
			
			BasicSqlService  bSqlServ = new BasicSqlService();
			
			String sql = servSql.getbykeySql();			
					
			list = bSqlServ.dbRead(cn, sql, fields, new DbGFollForumService());
			
			if(list.size() ==0){
				common.setAppFailMsg(msg , " 取记录失败，请检查！");
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;				
			}
			
			BaseGFollForum base = (BaseGFollForum)list.get(0);
				
			BeanUtils.copyProperties(theForm, base);
			
			DbcpPool.returnConnection(cn);
			return;
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about invite" + ex);
			common.setAppFailMsg(msg , ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return ;
	}
	/**
	 * 添加回复
	 * @see net.IUserbasicService.module.other.IUserService#add(net.swift.common.ReturnMessage)
	 */
	public void add(GFollForumForm theForm, ReturnMessage msg) 
	{
		
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			
			DbcpPool.beginTran(cn);

			//更新回复数
			List list = null;

			GForumServiceSqlMysql forumservSql = new GForumServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();

			//取得FORM中传入的查询参数
			Object[] fields3 = new Object[1];
			
			fields3[0] = theForm.getGForumID();			
			
			String sql = forumservSql.getbykeySql();			
					
			list = bSqlServ.dbRead(cn, sql, fields3, new DbGForumService());			
			if(list.size() ==0){
				common.setAppFailMsg(msg , " 取记录失败，请检查！");
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;				
			}
			BaseGForum base = (BaseGForum)list.get(0);
			
			//更新点击次数
			Object[] fields2 = new Object[5];
			
			fields2[0] = base.getAttachcnt();
			fields2[1] = base.getReplycnt() + 1;
			fields2[2] = base.getUpCnt();
			fields2[3] = base.getDownCnt();
			fields2[4] = base.getGForumID();
			
			sql = forumservSql.getUpdAttachReplycntSql();
						
			boolean  res = bSqlServ.dbWrite(cn, sql, fields2);
			if(!res){
				common.setAppFailMsg(msg , " 更新浏览次数失败！");
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return ;
			}	
			GFollForumServiceSqlMysql servSql = new GFollForumServiceSqlMysql();
			
		
			//判断回复者是否是第一次回复，如果是则更新积分
			Object[] fields4 = new Object[2];
			
			fields4[0] = theForm.getUserID();
			fields4[1] = theForm.getUserID();
	
			sql = servSql.getByOnlyUserIDSql();			
			
			List userlist = bSqlServ.dbRead(cn, sql, fields4, new DbGFollForumService());
			
			//保存附件一
			if(theForm.getFilepath() != null&&!theForm.getFilepath().getFileName().equals("")){
				
				FilesForm fileForm = new FilesForm();
				fileForm.setFormFile(theForm.getFilepath());
				fileForm.setF_info(theForm.getTitle());
				fileForm.setF_Zip(GlbFinal.ZIP_600);
				
				FilesServiceImpl fileservice = new FilesServiceImpl();
				
				fileservice.add(cn, fileForm, msg);
				if(msg.getErrCode() != 0){
					return;
				}
				theForm.setFileID(fileForm.getF_ID());
			}	
			
			//取得FORM中传入的查询参数
			Object[] fields = new Object[11];
			
			if(theForm.getGFollowID() == 0){
			
				//回复原帖 产生新回复序号
				theForm.setGFollowID(SequenceMgr.generateSeq(GlbFinal.SEQ_INITDATE, GlbFinal.SEQ_GFOLLOWID));
			}else{
				
				//回复跟帖 产生新的再跟帖序号
				int seq = getUpFollID(cn, theForm.getGFollowID(), msg);
				if(msg.getErrCode() !=0)  return;
				theForm.setGupFollID(seq);
			}
			
			fields[0] = theForm.getGFollowID();			
			fields[1] = theForm.getGupFollID();
			fields[2] = theForm.getGForumID();
			fields[3] = theForm.getGroupID();
			fields[4] = theForm.getTitle();
			fields[5] = theForm.getContent();
			fields[6] = theForm.getFileID();
			fields[7] = theForm.getUpUserID();
			fields[8] = theForm.getUserID();
			fields[9] = theForm.getIP();
			fields[10] = DatetimeUtil.getDatetime();
			
			sql = servSql.getInsertSql();
						
			res = bSqlServ.dbWrite(cn, sql, fields);
			if(!res){
				common.setAppFailMsg(msg , " 登记回复失败！");
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return ;
			}
			
			if(userlist.size() == 0){
				//更新话题发起者积分
				JifenServiceImpl jifenservice = new JifenServiceImpl();
				
				jifenservice.compjf(cn, 15, base.getUserID(), msg);
				if(msg.getErrCode() != 0){
					return;
				}				
				//更新回复者积分
				jifenservice.compjf(cn, 16, theForm.getUserID(), msg);
				if(msg.getErrCode() != 0){
					return;
				}					
			}
			
			DbcpPool.commitTran(cn);
			DbcpPool.closeTran(cn);
			DbcpPool.returnConnection(cn);

		} catch(Exception ex) 
		{
			common.setAppFailMsg(msg , ex.getMessage());
			logger.error("System error , param get about operlog" + ex);
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return ;
	}
	/**
	 * 取跟帖序号
	 * 	 * @see net.IUserbasicService.module.other.IUserService#update(net.swift.common.ReturnMessage)
	 */
	public int getUpFollID(Connection cn, int gFollowID, ReturnMessage msg) 
	{
		int gupFollID = 0;
		try
		{

			//取得FORM中传入的查询参数
			Object[] fields = new Object[1];
			
			fields[0] = gFollowID;
			
			GFollForumServiceSqlMysql servSql = new GFollForumServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			String sql = servSql.getByFollIDSql();
			
								
			List list = bSqlServ.dbRead(cn, sql, fields, new DbGFollForumService());
			
			if(list.size() ==0){
				common.setAppFailMsg(msg , " 取跟帖失败，请检查！");
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return gupFollID;				
			}
			BaseGFollForum base = (BaseGFollForum)list.get(0);
			
			gupFollID = base.getGupFollID();
			
			
			gupFollID += 1;
			
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about invite" + ex);
			common.setAppFailMsg(msg , ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} 
		
		return gupFollID;
	}

	/**
	 * 删除一条指定记录
	 * @see net.IUserbasicService.module.other.IUserService#update(net.swift.common.ReturnMessage)
	 */
	public void delete(GFollForumForm theForm, ReturnMessage msg) 
	{
		
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			DbcpPool.beginTran(cn);

			//取得FORM中传入的查询参数
			Object[] fields = new Object[2];
			
			fields[0] = theForm.getGFollowID();			
			fields[1] = theForm.getGupFollID();			
			
			GFollForumServiceSqlMysql servSql = new GFollForumServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			
			String sql = servSql.getbykeySql();			
					
			List list = bSqlServ.dbRead(cn, sql, fields, new DbGFollForumService());
			
			if(list.size() ==0){
				common.setAppFailMsg(msg , " 取记录失败，请检查！");
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;				
			}
			BaseGFollForum base = (BaseGFollForum)list.get(0);		
			BeanUtils.copyProperties(theForm, base);
			
			//删除附件
			if(base.getFileID() != 0){
				FilesServiceImpl fileservice = new FilesServiceImpl();
				FilesForm filesForm = new FilesForm();
				filesForm.Clear();
				filesForm.setF_ID(base.getFileID());
				fileservice.delete(cn, filesForm, msg);
				if(msg.getErrCode() != 0){
					return;
				}
			}			
			
			sql = servSql.getDeleteSql();
								
			boolean  res = bSqlServ.dbWrite(cn, sql, fields);
			if(!res){
				common.setAppFailMsg(msg , " 更新记录失败，请检查！");
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;
			}
			
			DbcpPool.commitTran(cn);
			DbcpPool.closeTran(cn);
			DbcpPool.returnConnection(cn);
			return;
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about invite" + ex);
			common.setAppFailMsg(msg , ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return ;
	}
	/**
	 * 删除所有回复记录
	 * @see net.IUserbasicService.module.other.IUserService#update(net.swift.common.ReturnMessage)
	 */
	public void delall(Connection cn, GFollForumForm theForm, ReturnMessage msg) 
	{
		
		try
		{

			//取得FORM中传入的查询参数
			Object[] fields = new Object[3];
			
			fields[0] = theForm.getGForumID();
			fields[1] = 0;
			fields[2] = 10000;
			
			GFollForumServiceSqlMysql servSql = new GFollForumServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();

			String  sql = servSql.getRecbyPageSql();			
			
			List list = bSqlServ.dbRead(cn, sql, fields, new DbGFollForumService());
			
			if(list.size() ==0){
				return;				
			}			
			int len = list.size();
			
			//删除所有回复的附件
			BaseGFollForum base = null;
			FilesServiceImpl fileservice = new FilesServiceImpl();
			FilesForm filesForm = new FilesForm();
			
			for(int i=0; i<len; i++){
				base = (BaseGFollForum)list.get(i);
				if(base.getFileID() != 0){
					filesForm.Clear();
					filesForm.setF_ID(base.getFileID());
					fileservice.delete(cn, filesForm, msg);
					if(msg.getErrCode() != 0){
						return;
					}
					
				}
			}
			//取得FORM中传入的查询参数
			Object[] fields1 = new Object[1];
			
			fields1[0] = theForm.getGFollowID();
			
			sql = servSql.getDelbygForumIDSql();
								
			boolean  res = bSqlServ.dbWrite(cn, sql, fields1);
			if(!res){
				common.setAppFailMsg(msg , " 删除记录失败，请检查！");
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;
			}
			
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about invite" + ex);
			common.setAppFailMsg(msg , ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
		}
		
		return ;
	}
	/**
	 * 更新一条指定记录
	 * @see net.IUserbasicService.module.other.IUserService#update(net.swift.common.ReturnMessage)
	 */
	public void update(GFollForumForm theForm, ReturnMessage msg) 
	{
		
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			
			DbcpPool.beginTran(cn);
			
			//上传附件
			if(theForm.getFilepath() != null && !theForm.getFilepath().getFileName().equals("")){
				
				FilesServiceImpl fileservice = new FilesServiceImpl();
				FilesForm fileForm = new FilesForm();
				
				fileForm.setF_ID(theForm.getFileID());
				fileForm.setFormFile(theForm.getFilepath());
				fileForm.setF_info(theForm.getTitle());
				
				fileservice.update(cn, fileForm, msg);
				if(msg.getErrCode() != 0){
					return;
				}
				theForm.setFileID(fileForm.getF_ID());
			}				

			//取得FORM中传入的查询参数
			Object[] fields = new Object[11];
			
			fields[0] = theForm.getGForumID();
			fields[1] = theForm.getGroupID();
			fields[2] = theForm.getTitle();
			fields[3] = theForm.getContent();
			fields[4] = theForm.getFilepath();
			fields[5] = theForm.getUpUserID();
			fields[6] = theForm.getUserID();
			fields[7] = theForm.getIP();
			fields[8] = DatetimeUtil.getDatetime();
			fields[9] = theForm.getGFollowID();
			fields[10] = theForm.getGupFollID();
			
			GFollForumServiceSqlMysql servSql = new GFollForumServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			
			String sql = servSql.getUpdateSql();
						
			boolean  res = bSqlServ.dbWrite(cn, sql, fields);
			if(!res){
				common.setAppFailMsg(msg , " 登记操作日志失败！");
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return ;
			}
			DbcpPool.commitTran(cn);
			DbcpPool.closeTran(cn);
			DbcpPool.returnConnection(cn);

		} catch(Exception ex) 
		{
			common.setAppFailMsg(msg , ex.getMessage());
			logger.error("System error , param get about operlog" + ex);
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return ;
	}

}
